<template>
    <div v-if="token == null">
        <el-empty description="您还未登录"></el-empty>

    </div>

    <el-card style="max-width: 600px;margin: auto;"v-else>
        <!-- 卡片头部 -->
        <template #header>
            <div class="card-header">
                <span style="margin-left:200px;">考试预约信息填写</span>
            </div>

        </template>
        <br>

        <div style="margin-left: 150px;">
            考试id：&nbsp;&nbsp;&nbsp;&nbsp;<el-input v-model="test_id" style="width: 240px"
                placeholder="根据当前记录填写考试id" /><br><br>
            学员id：&nbsp;&nbsp;&nbsp;&nbsp;<el-input v-model="student_id" style="width: 240px"
                placeholder="请输入您的学员id" /><br><br>
            考试场地：<el-input v-model="ground" style="width: 240px" placeholder="请输入预约考试场地" /><br><br>
            训练科目：<el-input v-model="subject_id" style="width: 240px" placeholder="输入训练科目数字（1,2,3,4）" /><br><br>
            考试时间：<el-date-picker style="width: 240px" v-model="session" type="date" placeholder="选择日期"
                format="YYYY/MM/DD" value-format="YYYY-MM-DD" :disabled-date="disabledDate" />
        </div>
        <!-- 卡片底部 -->
        <template #footer>
            <div style="margin-left: 200px;">
                <el-button round>取消</el-button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <el-button type="primary" round @click="enter()">确认</el-button>
            </div>
        </template>
    </el-card>


</template>

<script setup>
import { ref, onMounted, onUnmounted, } from 'vue';
import axios from 'axios';
import router from '@/router';
const test_id = ref('')
const student_id = ref('')
const ground = ref('')
const subject_id = ref('')
const session = ref('')
const token = localStorage.getItem('token')
const disabledDate = (time) => {
    // 获取今天 00:00:00 的时间戳
    const today = new Date();
    today.setHours(0, 0, 0, 0);

    // 禁用条件：选择的日期 <= 今天零点
    return time.getTime() <= today.getTime();
}
const info = ref({
    test_id: test_id,
    student_id: student_id,
    ground: ground,
    subject_id: subject_id,
    session: session,
})
function enter() {
    if (test_id.value=='' || student_id.value=='' || ground.value == '' || subject_id.value=='' || session.value== ''){
        alert('预约表内容不能为空')
    }

    axios.post('http://127.0.0.1:8000/driver_school/add_test/', info.value, {
        headers: {
            token: token
        }
    })
        .then(res => {
            console.log(res)
            alert('预约成功！')
            router.push("test")
        })
        .catch(err => {
            console.error(err);

        })
    onMounted(() => {
        // Your code here
    });
}


// 在组件卸载前执行的操作
onUnmounted(() => {
    // Your code here
});
</script>